<template>
  <div id="app-container">
    <router-link to="/league/list">
      <el-button type="primary">返回</el-button>
    </router-link>

    <el-button type="primary" @click="handleCardAdd">添加支持会员卡</el-button>

    <el-table :data="cardList" fit highlight-current-row>
      <el-table-column prop="name" label="会员卡名称" ></el-table-column>
      <el-table-column prop="type" label="会员卡类型" >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">次卡</span>
          <span v-if="scope.row.type == 1">储蓄卡</span>
        </template>
      </el-table-column>
      <el-table-column prop="gymName" label="支持场馆" ></el-table-column>
    </el-table>

    <!-- Dialog：添加和修改视频表单 -->
    <el-dialog :visible.sync="dialogFormVisible" title="视频维护">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="name" label="会员卡名称" ></el-table-column>
        <el-table-column prop="type" label="会员卡类型" >
          <template slot-scope="scope">
            <span v-if="scope.row.type == 0">次卡</span>
            <span v-if="scope.row.type == 1">储蓄卡</span>
          </template>
        </el-table-column>
        <el-table-column prop="gymName" label="支持场馆" ></el-table-column>
        <el-table-column prop="price" label="扣费" ></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>
import card from "../../api/card";
import league from "../../api/league";

  export default {
    data(){
      return{
        cardList: [],
        tableData: [],
        multipleSelection: [],
        dialogFormVisible: false,
        leagueId: ""
      }
    },
    created() {
      if(this.$route.params && this.$route.params.id){
        this.leagueId = this.$route.params.id
        this.queryByLeagueId(this.$route.params.id);
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = [];
        val.forEach((row) => {
          this.multipleSelection.push(row.id);
          console.log(this.multipleSelection)
        });
      },
      queryByLeagueId(leagueId){
        card.getByLeagueId(leagueId).then(res => {
          this.cardList = res.data
        })
      },
      handleCardAdd(){
        this.dialogFormVisible = true

        card.getCard(this.leagueId).then(res => {
          this.tableData = res.data
        })
      },
      handleSubmit(){
        if(this.multipleSelection && this.multipleSelection.length > 0){
          league.addCardRelation(this.leagueId, this.multipleSelection).then(res => {
            this.$message.success(res.message)
            this.dialogFormVisible = false;
            this.queryByLeagueId(this.leagueId)
          })
        }
      }
    }
  }
</script>
